package jp.melodroid.sample.sample_grid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.ArrayAdapter;

public class SampleGrid extends Activity
{
    /** Called when the activity is first created. */
    private int mResource[] = {
          R.drawable.sample_0,
          R.drawable.sample_1,
          R.drawable.sample_2,
          R.drawable.sample_3,
          R.drawable.sample_4,
          R.drawable.sample_5,
          R.drawable.sample_6,
          R.drawable.sample_7,
    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);

        ImageAdapter adapter = new ImageAdapter(this);
        adapter.setImage(mResource);
        gridview.setAdapter(adapter);

    }

    public class ImageAdapter extends ArrayAdapter<ImageView> {
        Context mContext;

        public ImageAdapter(Context c) {
           super(c,R.string.app_name);
           mContext = c;
        }

        public void setImage(int idList[]) {
           //set view
           ImageView view;

           for(int id:idList) {
              view = new ImageView(mContext);
	      //view.setScaleType(ImageView.ScaleType.CENTER_CROP); 
	      view.setImageResource(id);
              this.add(view);
           }
        }

        public ImageView getView(int position, View convertView, ViewGroup parent) {
	    ImageView v = getItem(position);
	    return v;
	}
    }
}
